//
//  NSString+Tools.m
//  CreditGroup
//
//  Created by JPlay on 14/10/21.
//  Copyright (c) 2014年 JPlay. All rights reserved.
//

#import "NSString+Tools.h"

@implementation NSString (Tools)

-(NSMutableAttributedString *)attributedStringFromStingWithFont:(UIFont *)font
                                                withLineSpacing:(CGFloat)lineSpacing{
    NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString:self attributes:@{NSFontAttributeName:font}];
    
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:lineSpacing];
    
    [attributedStr addAttribute:NSParagraphStyleAttributeName
                          value:paragraphStyle
                          range:NSMakeRange(0, [self length])];
    return attributedStr;
}

-(CGSize)boundingRectWithSize:(CGSize)size
                 withTextFont:(UIFont *)font
              withLineSpacing:(CGFloat)lineSpacing{
    NSMutableAttributedString *attributedText = [self attributedStringFromStingWithFont:font
                                                                        withLineSpacing:lineSpacing];
    CGSize textSize = [attributedText boundingRectWithSize:size
                                                   options:NSStringDrawingUsesLineFragmentOrigin
                                                   context:nil].size;
    return textSize;
}

+ (NSString *)currentDateString{
    NSDate *date = [NSDate date];
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    dateFormat.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    return [dateFormat stringFromDate:date];
}

+ (NSString *)dateStringFrom1970:(long)timeInterval{
    return [self dateStringFrom1970:timeInterval formatter:@"yyyy-MM-dd HH:mm:ss"];
}

+ (NSString *)dateStringFrom1970:(long)timeInterval formatter:(NSString *)formatter{ //获取当前时间字符串
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:timeInterval];
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateFormat = formatter;
    
    return [dateFormatter stringFromDate:date];
}

+ (NSString *)dateString:(NSString *)dateString fromFormatter:(NSString *)fromFormatter toFormatter:(NSString *)toFormatter{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateFormat = fromFormatter;
    
    NSDate *date = [dateFormatter dateFromString:dateString];
    if (!date) return nil;
    
    NSDateFormatter *dateFormatter2 = [[NSDateFormatter alloc] init];
    dateFormatter2.dateFormat = toFormatter;
    
    
    return [dateFormatter2 stringFromDate:date];
    
}

+ (NSDate *)dateFromString:(NSString *)dateString formatter:(NSString *)fromFormatter{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateFormat = fromFormatter;
    
    return [dateFormatter dateFromString:dateString];

}


@end
